package nu.aop.common;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import nu.aop.model.GroupHitsVO;
import nu.aop.service.StatisticService;
import nu.board.model.CategoryVO;
import nu.board.model.GroupVO;
import nu.member.model.MemberVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class StatisticAspect {
	@Resource(name="statisticService")
	private StatisticService statisticService;
	
	private Log log=LogFactory.getLog(getClass());
	
	@Around("execution(public * nu.board.controller.BoardController.showPageList(..))")
	public Object increaseGroupHits(ProceedingJoinPoint point) throws Throwable{
		String className=point.getTarget().getClass().getName();
		String methodName = point.getSignature().getName();
		Object params[]=point.getArgs();
		HttpSession session = (HttpSession)params[0];
		CategoryVO cvo = (CategoryVO)params[1];
		MemberVO mvo = (MemberVO) session.getAttribute("login");
		List<GroupVO> list = mvo.getList();
		for(int i=0;i<list.size();i++ ){
			if(cvo.getGroup_id()==(list.get(i).getGroup_id())){
				//세션에 담겨있는 그룹과 그룹에 담겨있는 그룹이 같을경우 if else 문을 돌자
				if(statisticService.selectGroupHitsById(mvo.getId())==null){
					statisticService.insertGroupHits(new GroupHitsVO(mvo.getId(),0,0,0,0,0));
				}
				if(cvo.getGroup_type().equals("study")){
					statisticService.increaseStudyHits(mvo.getId());
				}else if(cvo.getGroup_type().equals("finance")){
					statisticService.increaseFinanceHits(mvo.getId());
				}else if(cvo.getGroup_type().equals("sports")){
					statisticService.increaseSportsHits(mvo.getId());
				}else if(cvo.getGroup_type().equals("smoking")){
					statisticService.increaseSmokingHits(mvo.getId());
				}else if(cvo.getGroup_type().equals("living")){
					statisticService.increaseLivingHits(mvo.getId());
				}
			}
		}
		log.info(statisticService.selectGroupHitsById(mvo.getId()));
		Object retValue=null;
		retValue=point.proceed();
		return retValue;
	}
	@Around("execution(public * nu.member.service.MemberServiceImpl.getMemberInfo(..))")
	 public Object statisticPoint(ProceedingJoinPoint point) throws Throwable{
	  String className=point.getTarget().getClass().getName();
	  String methodName = point.getSignature().getName();
	  Object params[]=point.getArgs();
	  Object retValue=null;
	  try {
	   retValue = point.proceed();
	   MemberVO mvo = (MemberVO) retValue;
	   if (mvo != null) {
	    statisticService.confirmMemberPoint(mvo);
	   }
	  } finally {
	   log.debug("Rank "+className + " " + methodName + " 매개변수값 : "
	  + params[0].toString() + " 리턴값 : " + retValue);
	  }
	  return retValue;
	 }
	//@Around("execution(public * nu.board.model.BoardServiceImpl.login(..))")
	public Object example3(ProceedingJoinPoint point) throws Throwable{
		String className=point.getTarget().getClass().getName();
		String methodName = point.getSignature().getName();
		Object params[]=point.getArgs();
		Object retValue=null;
		return retValue;
	}
}
